<?php
require './thumbs.php';

function post($db) {
    if (!isset($_POST['comment'])){
        return array('status' => 'comment is missing');
    }
    else if (!isset($_POST['sessionId'])){
        return array('status' => 'sessionId is missing');
    }
    else if (!isset($_POST['latitude'])){
        return array('status' => 'latitude is missing');
    }
    else if (!isset($_POST['longitude'])){
        return array('status' => 'longitude is missing');
    }
    else if (empty($_FILES['picture'])){
        return array('status' => 'picture file is missing');
    }
    else {        
        $a = session_id($_POST['sessionId']);
        if (!empty($a)){
          return array('status' => 'session id is wrong');
        }
        session_start();
        $userCheck = new UserRepository();
        if ($userCheck->checkUserExistance($db, $_SESSION['username'])) {
            $addMessage = new MessageRepository();
            $newFileName = array_pop(explode('/',tempnam('./pictures','')));
			$file = $_FILES['picture']['tmp_name'];
//			$im = imagecreatefromstring($file);

            if (!$file) {
                          return array('status' => 'fileproblem');
            }
            if (!move_uploaded_file($file, './pictures/'.$newFileName)) {
                return array('status' => 'file not safe');                
            }
            $message = new Message(0, $userCheck->getUsersID($db, $_SESSION['username']), $_POST['comment'],
                    $newFileName, $_FILES['picture']['type'],
                    date('r', time()),$_POST['latitude'],$_POST['longitude']);
            $addMessage->addMessage($db, $message);            
            createThumb($newFileName, './thumbs/', 275);
            return array('status' => '1');            
        } else {
             return array('status' => 'user doesnt exist');
        }
    }
}
?>
